package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域配置 类
 *
 * @author zr
 * @date 2022/9/1 21:31
 */
@Configuration
public class GuliMallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        // 创建跨域配置
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许请求头
        corsConfiguration.addAllowedHeader("*");
        // 所有请求方式
        corsConfiguration.addAllowedMethod("*");
        // 运行请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 运行携带cookie
        corsConfiguration.setAllowCredentials(true);
        // 注册跨域配置
        configurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(configurationSource);
    }
}
